package com.sunnada.edu.system.login.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sunnada.edu.system.login.dao.LoginDao;
import com.sunnada.edu.system.pub.common.Constants;
import com.sunnada.edu.system.pub.util.Utils;

@Service
@Transactional
public class LoginServiceImpl implements LoginService {
	@Resource
	LoginDao loginDao;

	public Map getUserByUserId(HashMap<String, String> params) {
		return loginDao.getUserByUserId(params);
	}

	@Override
	public String getUserMenus(HashMap<String, String> params) {
		// <li class="layui-nav-item">
		// <a href="#">
		// <i class="fa fa-user-circle-o"></i>
		// <font>用户管理</font>
		// </a>
		// <dl class="layui-nav-child">
		// <dd><a href="#"
		// onclick="goPage('/agentController/user.shtml','25941');">添加用户</a></dd>
		// <dd><a href="#">更新用户</a></dd>
		// <dd><a href="#">删除用户</a></dd>
		// </dl>
		// </li>

		List<HashMap<String, String>> menuList = loginDao.getUserMenus(params);
		String menu = "";
		String parentId = "0";
		menu = joinMenuFrist(menuList, parentId, params);
		return menu;
	}

	private String joinMenuFrist(List<HashMap<String, String>> menuList, String parentId, HashMap<String, String> params) {
		String menu = "";
		for (HashMap<String, String> list : menuList) {
			if (list.get("PARENT_ID").equals(parentId)) {
				menu += "<li class='layui-nav-item'>";
				if (list.get("IS_MENU").equals("1")) {
					menu += "<a href='#' id='" + list.get("REC_ID") + "' onclick=goPage('" + list.get("LINK_PATH") + "','"
							+ list.get("REC_ID") + "','"+ list.get("MODULE_NAME") + "');>";
				} else {
					menu += "<a href='#' id='" + list.get("REC_ID") + "'>";
				}
				if (Utils.isNotNull(list.get("IMG"))) {
					if (list.get("IMG").indexOf("class") != -1) {
						String img[] = list.get("IMG").split("=");
						menu += "<i " + img[0] + "='" + img[1] + "'></i>";
					} else {
						menu += "<i>" + "<img src='" + params.get("path") + "/" + list.get("IMG") + "'/>" + "</i>";
					}
				}
				menu += "<font>" + list.get("MODULE_NAME") + "</font>";
				menu += "</a>";
				menu += joinMenuTwo(menuList, list.get("MODULE_CODE"), params);
				menu += "</li>";
			}
		}
		return menu;
	}

	private String joinMenuTwo(List<HashMap<String, String>> menuList, String parentId, HashMap<String, String> params) {
		String menu = "<dl class='layui-nav-child'>";
		for (HashMap<String, String> list : menuList) {
			if (list.get("PARENT_ID").equals(parentId)) {
				if (list.get("IS_MENU").equals("1")) {
					menu += "<dd><a href='#' id='" + list.get("REC_ID") + "' onclick=goPage('" + list.get("LINK_PATH") + "','"
							+ list.get("REC_ID") + "','"
							+ list.get("MODULE_NAME") + "');>" + list.get("MODULE_NAME") + "</a></dd>";
				} else {
					menu += "<dd><a href='#' id='" + list.get("REC_ID") + "'>" + list.get("MODULE_NAME") + "</a></dd>";
				}
			}
		}
		menu += "</dl>";
		return menu;
	}

}
